<!doctype html>
<!--
Copyright 2015 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="description" content="Sample illustrating the use of WebAudio AudioContext close.">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>WebAudio AudioContext close Sample</title>

    <!-- Add to homescreen for Chrome on Android -->
    <meta name="mobile-web-app-capable" content="yes">
    <link rel="icon" sizes="192x192" href="../images/touch/chrome-touch-icon-192x192.png">

    <!-- Add to homescreen for Safari on iOS -->
    <meta name="apple-mobile-web-app-title" content="WebAudio AudioContext close Sample">

    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" href="../images/apple-touch-icon-precomposed.png">

    <!-- Tile icon for Win8 (144x144 + tile color) -->
    <meta name="msapplication-TileImage" content="images/touch/ms-touch-icon-144x144-precomposed.png">
    <meta name="msapplication-TileColor" content="#3372DF">

    <link rel="icon" href="../images/favicon.ico">

    <link rel="stylesheet" href="../styles/main.css">
  </head>

  <body>
    <h1>WebAudio AudioContext close Sample</h1>

    <p>Available in <a href="https://www.chromestatus.com/feature/5411681005993984">Chrome 43+</a></p>

    <p>
      The <code>AudioContext.close</code> method closes the audio context, stopping audio operations, and releases any system resources in use.
      It will not necessarily release objects <em>created</em> by this <code>AudioContext</code>.
      This method returns a <code>Promise</code> that resolves when any resources blocking further creation of other audio contexts have been released.
    </p>

    <div class="output">
      <div>
        Manages a pool of AudioContext instances, allowing it to be grown or shrunk (by explicitly closing a random context).
        If your browser prevents creation of further contexts, close one first.
      </div>
      <pre id="count"></pre>
      <button id="btn-create">Create</button>
      <button id="btn-close">Close</button>
      <pre id="results" style="color: red"></pre>
    </div>

    <!-- // [START code-block] -->
    <script>

    // Create a large pool of initial contexts. On Mac OS X running M44, this
    // can create six contexts.
    var pool = (function(max) {
      var pool = [];
      var context;

      while (pool.length < max) {
        try {
          context = new AudioContext();
        } catch (e) {
          break;  // give up, can't create more contexts
        }
        pool.push(context);
      }

      return pool;
    }(100));

    function refreshCountMessage(opt_message) {
      count.textContent = 'pool: ' + pool.length + ' AudioContext instances';
      results.textContent = opt_message || '';
    }
    refreshCountMessage();

    function closeRandomContext() {
      if (pool.length > 0) {
        var choice = Math.floor(Math.random() * pool.length);
        var context = pool[choice];

        context.close().then(function() {
          // Find the context inside pool again, as its index may have changed
          // from 'choice' before the Promise resolved.
          pool.splice(pool.indexOf(context), 1);
          refreshCountMessage('close promise resolved');
        }).catch(function(e) {
          e = e || 'unknown';
          refreshCountMessage('close promise rejected: ' + e.toString());
        });
      }
    }

    function createContext() {
      var context;
      try {
        context = new AudioContext();
      } catch (e) {
        refreshCountMessage(e.toString());
        return;
      }
      pool.push(context);
      refreshCountMessage();
    }

    document.getElementById('btn-close').addEventListener('click', closeRandomContext);
    document.getElementById('btn-create').addEventListener('click', createContext);

    </script>
    <!-- // [END code-block] -->

    <script>
      /* jshint ignore:start */
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-53563471-1', 'auto');
      ga('send', 'pageview');
      /* jshint ignore:end */
    </script>
    <!-- Built with love using Web Starter Kit -->
  </body>
</html>
